import 'package:flutter_module/page/welfare_ranking/welfare_ranking_home_logic.dart';
import 'package:flutter_module/res/colors.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import '../../common/utils/aliyunImage.dart';
import '../../model/welfare_team_list_info.dart';
import '../../res/image.dart';

class WelfareRankingListItem extends StatelessWidget {
  final currentLogic = Get.find<WelfareRankingHomeLogic>();

  final WelfareTeamListInfo user;
  final bool isMysel;

  WelfareRankingListItem({super.key,required this.user, this.isMysel = false,});

  @override
  Widget build(BuildContext context) {
    return _cell(user, isMysel);
  }

  Widget _cell(WelfareTeamListInfo user, bool isMysel) {
    bool isCountingType = currentLogic.state.countingType.value == 1;
    String content = isCountingType ? (user.time ?? "0") : (user.hot ?? "0");
    return InkWell(
      onTap: () => currentLogic.selectBtnClicked(user),
      child: Container(
        constraints: BoxConstraints(minHeight: isMysel ? 60.0 : 50.0),
        color: isMysel ? YouthColors.color_F1FFE9 : Colors.white,
        child: Center(
          child: isMysel
              ? Row(
                  children: [
                    _rankNum(user, true),
                    _logo(user),
                    _textName(user, true),
                    _welfareTime(user, true),
                    content == "0"
                        ? _noWelfareHint(isCountingType)
                        : _welfareShareBtn(user)
                  ],
                  crossAxisAlignment: CrossAxisAlignment.center,
                )
              : Row(
                  children: [
                    _rankNum(user, false),
                    _logo(user),
                    _textName(user, false),
                    _welfareTime(user, false),
                  ],
                  crossAxisAlignment: CrossAxisAlignment.center,
                ),
        ),
      ),
    );
  }

  Widget _rankNum(WelfareTeamListInfo user, bool isMysel) {
    String num = '${user.rank ?? '99+'}';
    return Container(
      width: 40,
      margin: EdgeInsets.only(left: isMysel ? 15 : 12),
      child: Align(
        alignment: Alignment.center,
        child: Container(
            child: user.rank != null && user.rank! > 0 && user.rank! <= 3
                ? _rankNoImg(user.rank)
                : Container(
                    child: Text(
                      isMysel ? num : '${user.rank ?? 0}',
                      style: isMysel
                          ? TextStyle(
                              fontFamily: 'Bebas',
                              fontWeight: FontWeight.normal,
                              fontSize: 16,
                              color: YouthColors.color_449F11,
                            )
                          : TextStyle(
                              color: Color(0xFF17191D),
                              fontSize: 16,
                            ),
                    ),
                  )),
      ),
    );
  }

  Widget _logo(WelfareTeamListInfo user) {
    return Container(
      margin: EdgeInsets.only(left: 12),
      child: Align(
        alignment: Alignment.center,
        child: ClipRRect(
          child: FadeInImage.assetNetwork(
            placeholder: YouthImages.icDefaultHeader,
            image: YZAliyunImage.bmiddleImage(user.userLogo,
                placeholder: YouthImages.defaultHeaderUrl),
            width: 40,
            height: 40,
            fit: BoxFit.cover,
            // placeholderCacheWidth: 40,
            // placeholderCacheHeight: 40,
          ),
          borderRadius: BorderRadius.circular(20),
        ),
      ),
    );
  }

  Widget _noWelfareHint(bool isCountingType) {
    return GestureDetector(
      onTap: isCountingType
          ? () {
              currentLogic.jumpWelfareDo();
            }
          : null,
      child: Center(
        child: Container(
          margin: EdgeInsets.only(right: 35),
          child: Text(
            isCountingType ? '去做公益吧' : '去分享好事',
            style: TextStyle(
              color: Color(0xFF449F11),
              fontSize: 16,
            ),
          ),
        ),
      ),
    );
  }

  Widget _welfareShareBtn(WelfareTeamListInfo user) {
    return GestureDetector(
      onTap: () {
        currentLogic.shareWelfarePoster(user);
      },
      child: Container(
        margin: EdgeInsets.only(right: 20),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(25),
          gradient: LinearGradient(
            colors: [
              Color(0xFF74D112),
              Color(0xFF20C668),
            ],
            begin: Alignment.topLeft,
            end: Alignment.bottomRight,
          ),
        ),
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 12, vertical: 8),
          child: Text(
            '分享海报',
            style: TextStyle(
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  Widget _welfareTime(WelfareTeamListInfo user, bool isMysel) {
    return GetBuilder<WelfareRankingHomeLogic>(builder: (_) {
      bool isCountingType = currentLogic.state.countingType.value == 1;
      String content = isCountingType ? (user.time ?? "0") : (user.hot ?? "0");
      return Visibility(
        visible: content == "0" ? false : true,
        child: Padding(
          child: RichText(
            text: TextSpan(
              children: [
                TextSpan(
                  text: isCountingType ? '公益时长 ' : '人气值 ',
                  style: TextStyle(
                      fontSize: 10,
                      color: Color(0xFF17191D),
                      fontWeight: FontWeight.w400),
                ),
                TextSpan(
                  text: content,
                  style: TextStyle(
                    fontFamily: 'Bebas',
                    fontWeight: FontWeight.normal,
                    fontSize: 16,
                    color: Color(0xFF449F11),
                  ),
                ),
                if (isCountingType)
                  TextSpan(
                    text: 'h',
                    style: TextStyle(
                      fontSize: 16,
                      color: Color(0xFF449F11),
                    ),
                  ),
              ],
            ),
          ),
          padding: isMysel
              ? EdgeInsets.fromLTRB(10, 0, 0, 4)
              : EdgeInsets.fromLTRB(0, 0, 20, 4),
        ),
      );
    });
  }

  Widget _rankNoImg(int? rank) {
    String imageAsset;

    switch (rank) {
      case 1:
        imageAsset = YouthImages.icWelfareRankNo1;
        break;
      case 2:
        imageAsset = YouthImages.icWelfareRankNo2;
        break;
      case 3:
        imageAsset = YouthImages.icWelfareRankNo3;
        break;
      default:
        imageAsset = YouthImages.icWelfareRankNo1;
        break;
    }
    return Container(
      width: 30,
      height: 15,
      child: Image(image: AssetImage(imageAsset)),
    );
  }

  Widget _textName(WelfareTeamListInfo user, bool isMysel) {
    return Expanded(
      child: Container(
        margin: const EdgeInsets.only(left: 14),
        child: Text(
          // truncateString(user.userName ?? '', 4),
          user.userName ?? '',
          style: TextStyle(
              color: const Color(0xFF27312D),
              fontSize: isMysel ? 13 : 16,
              fontWeight: FontWeight.w500),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ),
    );
  }
}
